package com.spark.trouble

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.google.gson.Gson
import com.spark.common.BaseActivity
import com.spark.common.Const
import com.spark.common.ext.*
import com.spark.common.net.Http
import com.spark.common.net.toJsonBody
import com.spark.trouble.model.TroubleShootRecordModel
import kotlinx.android.synthetic.main.act_trouble_shoot_record.*

/**
 * 隐患排查记录
 */
class HTShootRecordActivity : BaseActivity() {
    private lateinit var tsrAdapter: TSRAdapter

    private var currPage = 1
    private var totalPage = 1

    companion object {
        fun actionStart(host: Activity) {
            host.startActivity(Intent(host, HTShootRecordActivity::class.java))
        }
    }

    override fun contentResId(): Int {
        return R.layout.act_trouble_shoot_record
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        iv_back.setOnClickListener { finish() }
        tv_title.text = "排查记录"

        iv_category.setOnClickListener {
            val diaShootRecordFragment = DiaHTFilterFragment.newInstance()
            diaShootRecordFragment.show(supportFragmentManager, "DiaSearchFragment")
            diaShootRecordFragment.addFilterListener {
                toast(it)
            }
        }
        tsrAdapter = TSRAdapter(R.layout.item_trouble_shoot_record).apply {
            setOnItemClickListener { adapter, view, position ->

            }
        }

        recy_trouble_shoot_records.apply {
            adapter = tsrAdapter
            layoutManager = LinearLayoutManager(context)
        }

        refresh_layout.apply {
            setOnRefreshListener {
                currPage = 1
                fetchTSR(currPage)
            }
            setOnLoadmoreListener {
                if (currPage >= totalPage) {
                    alert("没有更多数据了")
                } else {
                    currPage++
                    fetchTSR(currPage)
                }
            }
        }
        fetchTSR(currPage)
        loading()
    }

    @SuppressLint("CheckResult")
    private fun fetchTSR(page: Int) {
        /**
         * 其他参数
         * clauseKey: 隐患关键词
         * startTime: 主任务开始时间
         * endTime: 主任务结束时间
         * taskState: 任务状态
         */
        val params = mutableMapOf(
                Pair(Const.qr_pageNum, page.toString()),
                Pair(Const.qr_pageSize, "10")
        )
        Http.request()
                .rxPost("${Const.base_url}${Const.act_trouble_shoot_records}", params.toJsonBody())
                .wrapIO()
                .subscribe(
                        {
                            Gson().fromJson(it, TroubleShootRecordModel::class.java)?.apply {
                                if (code == "200") {
                                    totalPage = this.data.pageCount
                                    if (page == 1) {
                                        tsrAdapter.data.clear()
                                    }
                                    tsrAdapter.addData(this.resultMaps)
                                }
                            }
                        },
                        {
                            it.printStackTrace()
                            report(it)
                            hideLoader()
                            if (page == 1) refresh_layout.finishRefresh() else refresh_layout.finishLoadmore()
                        },
                        {
                            hideLoader()
                            if (page == 1) refresh_layout.finishRefresh() else refresh_layout.finishLoadmore()
                        },
                        {
                            dispose = it
                        }
                )
    }
}

class TSRAdapter(layoutRes: Int) : BaseQuickAdapter<TroubleShootRecordModel.ResultMap, BaseViewHolder>(layoutRes) {
    private var sdf = sdf()
    override fun convert(helper: BaseViewHolder?, item: TroubleShootRecordModel.ResultMap?) {
        if (item == null) return
        helper?.apply {
            setText(R.id.tv_content, item.clause_description)
            setText(R.id.tv_time, sdf.format(item.create_time))
            setText(R.id.tv_state, item.stateFlag)
            setGone(R.id.divider, adapterPosition != data.size - 1)
        }
    }

}